/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt-sample module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxtsample.client.editor;

import com.daikit.daikit4gxt.client.editor.AbstractDkEditedBeanAwareEditor;
import com.daikit.daikit4gxt.client.editor.DkListStoreRemovableEditor;
import com.daikit.daikit4gxt.client.ui.fields.DkHtmlEditor;
import com.daikit.daikit4gxt.client.ui.fields.DkReadOnlyField;
import com.daikit.daikit4gxt.client.ui.fields.RadioGroupField;
import com.daikit.daikit4gxt.client.ui.forms.DkMultipleValueEditorGrid;
import com.daikit.daikit4gxtsample.client.Main;
import com.daikit.daikit4gxtsample.client.propertyaccess.CountryDetailPropertyAccess;
import com.daikit.daikit4gxtsample.client.propertyaccess.ProductCommentsPropertyAccess;
import com.daikit.daikit4gxtsample.client.propertyaccess.ShopPropertyAccess;
import com.daikit.daikit4gxtsample.client.propertyaccess.SupplierDetailPropertyAccess;
import com.daikit.daikit4gxtsample.client.ui.field.LocaleEditor;
import com.daikit.daikit4gxtsample.shared.bean.CountryDetailData;
import com.daikit.daikit4gxtsample.shared.bean.ProductData;
import com.daikit.daikit4gxtsample.shared.bean.ProductExtendingSpecialData;
import com.daikit.daikit4gxtsample.shared.bean.ProductManager;
import com.daikit.daikit4gxtsample.shared.bean.ProductSpecialData;
import com.daikit.daikit4gxtsample.shared.bean.ProductVerySpecialData;
import com.daikit.daikit4gxtsample.shared.bean.ShopData;
import com.daikit.daikit4gxtsample.shared.bean.SupplierDetailData;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.editor.client.Editor;
import com.sencha.gxt.cell.core.client.form.TextInputCell;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor.DoublePropertyEditor;
import com.sencha.gxt.widget.core.client.form.TextField;


/**
 * An {@link Editor} for {@link ProductData}
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
@SuppressWarnings("javadoc")
public class ProductEditor extends AbstractDkEditedBeanAwareEditor<ProductData>
{

	//	private static final BaseLogger LOG = BaseLogger.getLog(ProductEditor.class);

	public final DkHtmlEditor propertyRichText = new DkHtmlEditor();
	public final TextField id = new TextField();
	public final TextField name = new TextField();
	public final TextField property1 = new TextField();
	public final TextField property2 = new TextField();
	public final TextField property3 = new TextField();
	public final TextField property4 = new TextField();
	public final RadioGroupField propertyBoolean = new RadioGroupField();
	public final NumberField<Double> propertyDouble = new NumberField<Double>(new DoublePropertyEditor());

	@Path("productDetail")
	public final ProductDetailEditor productDetailEditor = new ProductDetailEditor();

	public final DkReadOnlyField<ProductManager> productManager = new DkReadOnlyField<ProductManager>(
			new LabelProvider<ProductManager>()
			{
				@Override
				public String getLabel(final ProductManager productManager)
				{
					return productManager == null ? "" : productManager.getName();
				}
			});

	@Path("")
	@SuppressWarnings("unchecked")
	public final ProductSpecialSubTypeEditor productSpecialSubTypeEditor = new ProductSpecialSubTypeEditor(
			new ProductSpecialEditor(), ProductSpecialData.class, ProductExtendingSpecialData.class);
	@Path("")
	public final ProductVerySpecialSubTypeEditor productVerySpecialSubTypeEditor = new ProductVerySpecialSubTypeEditor(
			new ProductVerySpecialEditor(), ProductVerySpecialData.class);

	public final DkListStoreRemovableEditor<SupplierDetailData> supplierDetails = new DkListStoreRemovableEditor<SupplierDetailData>(
			SupplierDetailPropertyAccess.INSTANCE.key());

	public final LocaleEditor localizedProperty = new LocaleEditor(Main.model().cache().locales.getList());

	public final DkListStoreRemovableEditor<CountryDetailData> countryDetails = new DkListStoreRemovableEditor<CountryDetailData>(
			CountryDetailPropertyAccess.INSTANCE.key());

	public final DkListStoreRemovableEditor<ShopData> shops = new DkListStoreRemovableEditor<ShopData>(
			ShopPropertyAccess.INSTANCE.key());

	public final DkMultipleValueEditorGrid<String> comments = new DkMultipleValueEditorGrid<String>(
			ProductCommentsPropertyAccess.INSTANCE)
	{
		@Override
		public String getDeleteTooltip()
		{
			return Main.i18n().label_delete_this_entry();
		}

		@Override
		protected Cell<String> createCell()
		{
			return new TextInputCell();
		}

		@Override
		public void invalidateUi()
		{
			setEnabled(Main.model().isLogged() && isEditing());
		}
	};


	public boolean isSpecialProductData()
	{
		return getEditedModel() instanceof ProductSpecialData;
	}

	public boolean isVerySpecialProductData()
	{
		return getEditedModel() instanceof ProductVerySpecialData;
	}

}
